//
//  YDYLiveViewController.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/10/8.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYLiveViewController.h"

#import "YDYCyclePhotoView.h"
#import "YDYLiveTableHeaderView.h"
#import "YDYLiveTableViewSectionHeader.h"
#import "YDYLiveTableViewSectionFooter.h"
#import "YDYLiveTableViewCell.h"
#import "YDYNetWorkTool.h"
#import "YDYPartitionMessage.h"
#import <MJExtension.h>
#import "YDYPartition.h"
#import "YDYLiveContentModel.h"
#import "YDYLive.h"
#import "YDYNormalRefresh.h"
#import "YDYLiveViewController.h"
@interface YDYLiveViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 最基层的tableview */
@property (nonatomic, strong) UITableView *tableview;
/** 数据源 */
@property (nonatomic, strong) YDYLiveContentModel *liveContentModel;
/** 整个tableview顶部的视图 */
@property (nonatomic, weak) YDYLiveTableHeaderView *tableheaderView;

@end

static NSString * const ydy_tableviewCellIdentifier = @"tableviewCellIdentifier";
@implementation YDYLiveViewController

#pragma mark -
#pragma mark -数据源setter
-(void)setLiveContentModel:(YDYLiveContentModel *)liveContentModel{
    _liveContentModel = liveContentModel;
    self.tableheaderView.LiveContentModel = liveContentModel;
    
    
    
    [self.tableview reloadData];
}


#pragma mark -


- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    [self setupTableview];
    [self loadData];
    
    
}
-(void)loadData{
    
    [YDYNetWorkTool GET:@"http://live.bilibili.com/AppIndex/home?_device=android&_hwid=51e96f5f2f54d5f9&_ulv=10000&access_key=563d6046f06289cbdcb472601ce5a761&appkey=c1b107428d337928&build=410000&platform=android&scale=xxhdpi&sign=fbdcfe141853f7e2c84c4d401f6a8758" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
#warning write to plist
        YDYWriteToPlist(responseObject, @"bilibiliLive");
        NSDictionary *dict = [responseObject objectForKey:@"data"];
        
        YDYLiveContentModel *model = [YDYLiveContentModel mj_objectWithKeyValues:dict];
        self.liveContentModel = model;
        
        [self.tableview.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        YDYLog(@"error :%@",error);
        [self.tableview.mj_header endRefreshing];
    }];
    

}


-(void)setupTableview{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, YDYScreenSize.width, YDYScreenSize.height - (40 + 60)) style:UITableViewStyleGrouped];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, YDYScreenSize.width, YDYScreenSize.height  - 40) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.autoresizesSubviews = NO;
    [self.view addSubview:self.tableview];
    
    //直接设置会有问题，所以先取出修改再放回
    UIEdgeInsets inset = self.tableview.contentInset;
    inset.bottom += 60;
    self.tableview.contentInset = inset;
    
    
    //header view
    YDYLiveTableHeaderView *headerView  = [YDYLiveTableHeaderView viewFromXib];
    headerView.frame = CGRectMake(0, 0, self.tableview.bounds.size.width, 400);
    self.tableview.tableHeaderView = headerView;
    self.tableheaderView = headerView;
    
    //register
    [self.tableview registerNib:[UINib nibWithNibName:NSStringFromClass([YDYLiveTableViewCell class]) bundle:nil] forCellReuseIdentifier:ydy_tableviewCellIdentifier];
    
    
    //下拉刷新
    self.tableview.mj_header = [YDYNormalRefresh headerWithRefreshingTarget:self refreshingAction:@selector(pullToRefresh)];
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    //让顶部有弯角
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    maskLayer.frame = self.view.bounds;
    self.view.layer.mask = maskLayer;

}
                                
#pragma mark -
#pragma mark -下拉刷新
-(void)pullToRefresh{
    [self loadData];
    YDYLog(@"refresh");
}

#pragma mark -
#pragma mark -tableview delegate

    

#pragma mark -
#pragma mark - tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.liveContentModel.partitions.count == 0) {
        return 10;
    }else{
        return self.liveContentModel.partitions.count;
    }
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YDYLiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ydy_tableviewCellIdentifier];
    YDYPartition *partition = self.liveContentModel.partitions[indexPath.section];
    cell.partition = partition;
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    YDYLiveTableViewSectionHeader *header =[YDYLiveTableViewSectionHeader viewFromXib];
    if (self.liveContentModel==nil) return header;
    
    YDYPartition *partition = self.liveContentModel.partitions[section];
    header.partitionMessage = partition.partitionMessage;
    
    return header;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    YDYLiveTableViewSectionFooter *footer =[YDYLiveTableViewSectionFooter viewFromXib];
    
    if (self.liveContentModel == nil)return footer;
    
    YDYPartition *partition = self.liveContentModel.partitions[section];
    footer.partitionMessage = partition.partitionMessage;
    
    return footer;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 380;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}

@end
